// 用水走势卡
var timeDataMap = {
    'year': ["01","02","03","04","05","06","07","08","09","10","11","12"],
    'month': ["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"],
    'day': ["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23"],
    'hour': ["01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20",
        "21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40",
        "41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59"]
};
var timeFormatMap = {
    'year': "%Y-%m",
    'month': "%Y-%m-%d",
    'day': "%Y-%m-%d %H",
    'hour': "%Y-%m-%d %H:%m"
};
var timeSingleFormatMap = {
    'year': "%m",
    'month': "%d",
    'day': "%H",
    'hour': "%i"
};

dev.action = function(resp,param){
    //获取工厂名称
    var factoryName = getFactoryName(param.workSpaceName);
    //是否算总还是单
    var totalOrSingle = param.selectType;
    if (totalOrSingle == null){
        totalOrSingle = "total";
    }
    var theTime = param.time;
    var workSpace = param.workSpace;
    var db = iot.db();
    var format,singleFormate;

    var resultData = {
        xData : iot.list(),
        yLeft: iot.list(),
        yRight : iot.list(),
        yName : iot.list()
    };

    var timeType = iot.getTimeByFormat(theTime);
    var timeDataList = getTimeList(theTime);
    format = timeFormatMap[timeType];
    singleFormate = timeSingleFormatMap[timeType];

    var coldResult = db.qry("select DATE_FORMAT(dev_win.the_time,'" + format + "') time," +
        " DATE_FORMAT(dev_win.the_time,'" + singleFormate + "') timeIx," +
        " SUM(dev_win.dvalue) totalSum" +
        " from iot_dev_data_win dev_win, (select * from energy_struc_prop where type = 1 and field_type = '用量' and energy_struc_id = "+ workSpace +") esp" +
        " where dev_win.dev_id = esp.dev_id and dev_win.data_type = esp.name" +
        " and dev_win.the_time like '"+ theTime +"%' group by time");

    var yieldId = getFactoryYieldId(factoryName)
    var yieldResult = db.qry("select DATE_FORMAT(the_time,'" + format + "') time," +
        " DATE_FORMAT(the_time,'" + singleFormate + "') timeIx," +
        " SUM(dvalue) totalSum from iot_dev_data_win" +
        " where dev_id = "+ yieldId +" and the_time like '"+ theTime +"%' group by time");

    var planResult;
    if (totalOrSingle.equals("total") && timeType == 'year') {
        planResult = db.qry("select DATE_FORMAT(dev_win.the_time,'" + format + "') time," +
            " DATE_FORMAT(dev_win.the_time,'" + singleFormate + "') timeIx, SUM(dev_win.dvalue) totalSum" +
            " from iot_dev_data_win dev_win, (select * from energy_struc_prop where type = 1 and field_type = '目标值' and energy_struc_id = "+ workSpace +") esp" +
            " where dev_win.dev_id = esp.dev_id and dev_win.data_type = esp.name" +
            " and dev_win.the_time like '"+ theTime +"%' group by time");
    }

    // 通过timeType获取当前时间
    var result = getCurrentTimeByType(timeType);
    var typeTime = result["typeTime"];
    var currTime = result["currTime"];

    for(var ix in timeDataList){
        var timeItem = timeDataList[ix];
        var data  = 0;
        coldResult.forAll(function (item,ix) {
            if(item && item.timeIx && item.timeIx === timeItem){
                data = item.totalSum;
            }
        })
        var totalYield = 0;
        yieldResult.forAll(function (item) {
            if(item && item.timeIx && item.timeIx === timeItem){
                totalYield = item.totalSum;
            }
        });
        //判断当前选择总数还是单车
        if (totalOrSingle.equals("total")) {
            resultData.xData.add(timeItem);
            // 把当前时间之后的数据干掉
            if (theTime == currTime && timeItem > typeTime) {
                continue;
            }
            resultData.yLeft.add(data);
        }else if (totalOrSingle.equals("single")) {
            resultData.xData.add(timeItem);
            // 把当前时间之后的数据干掉
            if (theTime == currTime && timeItem > typeTime) {
                continue;
            }
            if (data && totalYield){
                data = data/totalYield;
                data = parseInt(data);
            }
            if (totalYield === 0){
                resultData.yLeft.add(0);
            }else{
                resultData.yLeft.add(data);
            }
        }

    }

    // 添加计划数据
    for(var ix in timeDataList){
        var timeItem = timeDataList[ix];
        var data  = 0;
        if (planResult) {
            planResult.forAll(function (item) {
                if (item && item.timeIx && item.timeIx === timeItem) {
                    data = item.totalSum;
                }
            })
        }
        // 把当前时间之后的数据干掉
        if (theTime == currTime && timeItem > typeTime) {
            continue;
        }
        resultData.yRight.add(data);
    }
    // 添加两条y轴的名称，单位
    if (totalOrSingle.equals("total")) {
        resultData.yName.add('实际 (t)');
        resultData.yName.add('计划 (t)');
    } else {
        resultData.yName.add('实际 (t/辆)');
        resultData.yName.add('计划 (t/辆)');
    }

    resp.setJsData(resultData);
}